﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace BezPanike.Domain.Services
{
    public class LinkService
    {
        readonly BezPanikeContextDataContext _context = new BezPanikeContextDataContext();

        public List<Linkovi> UzmiBrojLinkova(int number)
        {
            var query = (from linkovi in _context.Linkovi
                         orderby linkovi.PageRank descending
                         select linkovi).Take(number);

            return query.ToList();
        }

        public List<Linkovi> UzmiSlucajneLinkove(int number)
        {
            var rows = from links in _context.Linkovi
                       where links.FK_STATUS_ID == 3
                       select links;

            var count = rows.Count() - number;
            var index = new Random().Next(count);

            var randoms = rows.Skip(index).Take(number);

            return randoms.ToList();
        }

        public IList<Linkovi> PretraziLinkove(string text)
        {
            var query = (from linkovi in _context.Linkovi
                         where linkovi.Url.Contains(text)
                               || linkovi.Opis.Contains(text)
                               || linkovi.ImeFirme.Contains(text)
                         select linkovi).ToList();

            return query;
        }

        public IList<Linkovi> UzmiLinkovePoBrojuPoseta(int i)
        {
            var query = (from linkovi in _context.Linkovi
                         orderby linkovi.BrojPoseta descending
                         select linkovi).Take(i);

            return query.ToList();
        }
    }
}
